package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.SysAreaInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.SysAreaInfoService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Map;

/**
 * @author xy
 * @Description: 行政区划控制器
 * @date 2018/2/24 14:16
 * @version V1.0
 */
@RestController
@RequestMapping("area")
@Api(value="行政区划controller",tags={"行政区划操作接口"})
public class SysAreaInfoController {
    @Autowired
    private SysAreaInfoService areaInfoService;

    /**
     * 新增行政区划
     *
     * @param payload
     * @return
     */
    @ApiOperation(value = "新增行政区划", notes = "新增行政区划")
    @ApiResponses({@ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改时传的id", required = false, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "areaName", value = "行政区划名字", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "系统层级码", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "add")
    public ResultBean<SysAreaInfo> add(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "areaName", "areaCode");
        if (result.getCode() == 1) { return result.boolAdd(areaInfoService.save(payload));}
        return result;
    }

    /**
     * 根据id 删除行政区划
     *
     * @param payload
     */
    @ApiOperation(value = "删除行政区划", notes = "根据id删除行政区划")
    @ApiResponses({@ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "行政区划id", required = true, dataType = "String", paramType = "query"),
    })
    @DeleteMapping(value = "delete")
    public ResultBean<Boolean> delete(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == 1) {
              return new ResultBean<Boolean>(areaInfoService.deleteArea(payload.get("id").toString()));
        }
        return result;
    }


    /**
     * 查询所有行政区划
     *
     * @return
     */
    @ApiOperation(value = "查询所有行政区划", notes = "无参数获取所有数据")
    @ApiResponses({@ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足")})
    @GetMapping(value = "list")
    public ResultBean<Collection<SysAreaInfo>> queryArea() {
        return new ResultBean<Collection<SysAreaInfo>>(areaInfoService.findAllAreas());
    }
    /**
     * 通过id 根据id查询行政区划
     * @param payload
     * @return
     */
    @ApiOperation(value="根据id查询行政区划", notes="根据id查询行政区划")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "行政区划id", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping(value = "info")
    public ResultBean<SysAreaInfo> findAreaById(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == 1) {
            return new ResultBean<SysAreaInfo>(areaInfoService.findAreaById(payload.get("id").toString()));
        }
        return result;
    }

    /**
     * 分页查询行政区划
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页查询行政区划", notes="分页无参数查询行政区划")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping("paging")
    public ResultBean<Page<SysAreaInfo>> showAreas(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            Page<SysAreaInfo> pageAreaInfo = areaInfoService.findAll(Integer.parseInt(payload.get("pageIndex")
                    .toString()),Integer.parseInt(payload.get("pageSize").toString()));
            return new ResultBean<Page<SysAreaInfo>>(pageAreaInfo);
        }
        return result;
    }

    /**
     * 分页查询用户列表
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页带参数查询行政区划", notes="分页带参数查询行政区划")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "areaName", value = "行政区划", required = true, dataType = "String",paramType="query")
    })
    @GetMapping("paging/query")
    public ResultBean<Page<SysAreaInfo>> PageAreas(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize","areaName");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            SysAreaInfo areaInfo = new SysAreaInfo();
            areaInfo.setAreaName(payload.containsKey("areaName") ?payload.get("areaName") != null ? payload.get("areaName").toString():"":"" );
            Page<SysAreaInfo> pageAreaNameInfo = areaInfoService.findBookCriteria(Integer.parseInt(payload.get("pageIndex") .toString()),Integer.parseInt(payload.get("pageSize").toString()),areaInfo);
            return new ResultBean<Page<SysAreaInfo>>(pageAreaNameInfo);
        }
        return result;
    }
}
